
// <copyright file="FilteredDataReader.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The filtered data reader.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data
{
    using System.Data;

    using SubhadraSolutions.Sharp.Utils.Collections.Generic;
    using SubhadraSolutions.Sharp.Utils.Data.Common;

    /// <summary>
    /// The filtered data reader.
    /// </summary>
    public class FilteredDataReader : AbstractDataReaderDecorator
    {
        #region Fields

        /// <summary>
        /// The record filter.
        /// </summary>
        private readonly IFilter<IDataRecord> _recordFilter;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="FilteredDataReader"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        /// <param name="recordFilter">
        /// The record filter.
        /// </param>
        public FilteredDataReader(IDataReader adaptedObject, IFilter<IDataRecord> recordFilter)
            : base(adaptedObject)
        {
            this._recordFilter = recordFilter;
        }

        #endregion Constructors and Finalizers

        #region Public Methods and Operators

        /// <summary>
        /// The read.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool Read()
        {
            bool canRead = false;
            while (this.AdaptedObject.Read())
            {
                if (this._recordFilter.ShouldFilter(this.AdaptedObject))
                {
                    canRead = true;
                    break;
                }
            }

            return canRead;
        }

        #endregion Public Methods and Operators
    }
}